# Copyright (c) 2008-2009 Mikhail Chernov 
#
# This file is part of dcExplorer.
#
# dcExplorer is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# dcExplorer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with dcExplorer.  If not, see <http://www.gnu.org/licenses/>.

package dcExplorer::wxui::widget::Fleets;
use strict;
use Wx::Html;
use base 'Wx::SimpleHtmlListBox';
use Wx qw(wxHLB_MULTIPLE);
use Tools;

sub new
{
	my $proto = shift;
	my $parent = shift;
	my $id = shift;
	my $class = ref($proto) || $proto;
	my $self = $class->SUPER::new($parent, $id, [-1, -1], [-1, -1], [], wxHLB_MULTIPLE);
	$self->SetBackgroundColour(Wx::Colour->new(0, 0, 0));
	$self->{Fleets} = [];
	$self->{SharedFleets} = [];
	$self->{AlienFleets} = [];
	bless($self, $class);
	return $self;
}

sub clear
{
	my $self = shift;
	for (my $i = $self->GetCount() - 1; $i > -1; --$i)
	{
		$self->Toggle($i) if $self->IsSelected($i);
		$self->Delete($i);
	}
}

sub compare_fleet
{
	$a->getProp('turns_till_arrival') > $b->getProp('turns_till_arrival') ? 
		1 : 
		$a->getProp('turns_till_arrival') < $b->getProp('turns_till_arrival') ? 
			-1 :
			$a->getProp('name') lt $b->getProp('name') ? -1 : $a->getProp('name') gt $b->getProp('name') ? 1 : 0
}

sub format_fleet_name 
{
	$_ = shift;
	my ($space_ships, $carrier_capacity, $transport_capacity, $carried, $transported) = (0, 0, 0, 0, 0);
	foreach ($_->getAllUnits)
	{
		$space_ships += 1 unless $_->getProp('is_transportable');
		$carrier_capacity += $_->getProp('carrier_capacity');
		$transport_capacity += $_->getProp('transport_capacity');
		$carried += 1 if $_->getProp('is_transportable') == 1;
		$transported += 1 if $_->getProp('is_transportable') == 2;
	};
	return '<font color="'.($_->getProp('turns_till_arrival') ? 'gray' : 'white').'">'.$_->getProp('name').
		' ('.$space_ships.', '.
		($carrier_capacity ? $carried.'/'.$carrier_capacity : '0').', '.
		($transport_capacity ? $transported.'/'.$transport_capacity : '0').')</font>'
}

sub format_fleet_suffix
{
	$_ = shift;
	return 
		($_->getProp('hidden') || $_->getProp('fleet_changes') ?
			'<font color="'.($_->getProp('hidden') ? ($_->getProp('fleet_changes') ? 'red' : 'silver') : 'lime').'" size="-1"> h</font>' : ''
		).
		(defined($_->getProp('tag')) ? '<font color="red">'.$_->getProp('tag').'</font>' : '').
		($_->getProp('shares_count') ? 
			' <font color="cyan" size="-1">{'.$_->getProp('shares_count').'}</font>' : ''
		).
		($_->getProp('turns_till_arrival') ? 
			' <font color="white">'.$_->getProp('turns_till_arrival').'</font> <font color="gray">&lt;'.Tools::formatCoordinate($_->getProp('from_x')).':'.Tools::formatCoordinate($_->getProp('from_y')).'</font>' : ''
		)
}

sub setFleets
{
	my $self = shift;
	my $fleets = shift;
	$self->{Fleets} = [sort compare_fleet @$fleets];
	my $sharedFleets = shift;
	$self->{SharedFleets} = [sort compare_fleet @$sharedFleets];
	$self->{AlienFleets} = shift;
	$self->refresh;
}

sub refresh
{
	my $self = shift;
	my %colors = (
		-1 => "gray",
		0 => "red",
		1 => "blue",
		2 => "lime",
		3 => "cyan",
		4 => "yellow"
	);
	$self->clear();

	$self->Append([map {
		format_fleet_name($_).format_fleet_suffix($_)
	} @{$self->{Fleets}}]);

	$self->Append([map {
		my $empire = $_->getEmpire();
		my $id = $_->getProp('owner'); 
		my $rel = defined($id) ? $empire->dipRelation($id) : -1;
		$rel = -1 unless defined $rel;

		'<font color="'.$colors{$rel}.'">{</font>'.format_fleet_name($_).
		'<font color="'.$colors{$rel}.'">}</font>'.format_fleet_suffix($_).
		(defined($_->getProp('owner')) ? 
			'<font color="'.$colors{$rel}.'"> ('.$empire->players->get($id)->getProp('name').')</font>' : ''
		)

	} @{$self->{SharedFleets}}]);

	$self->Append(['<font color="white">---</font>']);

	$self->Append([map { 
		my $empire = $_->getEmpire();
		my $id = $_->getProp('player_id'); 
		my $rel = defined($id) ? $empire->dipRelation($id) : -1;
		$rel = -1 unless defined $rel;

		'<font color="'.($_->getProp('turns_till_arrival') ? "gray" : 'white').'">'.
		(defined($_->getProp('name')) ? 
			$_->getProp('name') : 
			'Unknown fleet'
		).'</font>'.
		'<font color="'.$colors{$rel}.'"> ('.
		(defined($_->getProp('turns_till_arrival')) ?
			$_->getProp('turns_till_arrival').',' : ''
		).
		$_->getProp('large_ship_count').','.$_->getProp('sum_weight').')</font>'.
		($_->getProp('hidden') ?
			'<font color="red" size="-1"><b> h'.($_->getProp('really_see') ? '' : '*').'</b></font>' : ''
		).
		($_->getProp('shared') ?
			' <font color="cyan" size="-1">[s]</font>' : ''
		).
		(defined($_->getProp('player_name')) ? 
			'<font color="'.$colors{$rel}.'"> ('.$_->getProp('player_name').')</font>' : ''
		).
		($_->getProp('turns_till_arrival') ? 
			' <font color="white">'.$_->getProp('turns_till_arrival').'</font> <font color="gray">&lt;'.$_->getProp('from_x').':'.$_->getProp('from_y').'</font>' : ''
		)
	} @{$self->{AlienFleets}}]);
}

sub getSelectedControlledFleets
{
	my $self = shift;
	my $res = [];
	for (my $i = 0; $i < scalar @{$self->{Fleets}}; ++$i)
	{
		push @{$res}, $self->{Fleets}->[$i] if $self->IsSelected($i);
	}
	for (my $i = 0; $i < scalar @{$self->{SharedFleets}}; ++$i)
	{
		push @{$res}, $self->{SharedFleets}->[$i] if $self->IsSelected($i + scalar @{$self->{Fleets}});
	}
	return $res;
}

sub getSelectedFleets
{
	my $self = shift;
	my $res = $self->getSelectedControlledFleets();
	for (my $i = 0; $i < scalar @{$self->{AlienFleets}}; ++$i)
	{
		push @{$res}, $self->{AlienFleets}->[$i] if $self->IsSelected($i + scalar @{$self->{Fleets}} + scalar @{$self->{SharedFleets}} + 1);
	}
	return $res;
}

1;
